package com.company.zhishidian.thread;

import java.util.Scanner;
import java.util.concurrent.*;

/**
 * Thread
 * 线程 共享资源
 * 进程 独立的资源
 * 多线程任务 并发
 *
 */
class XianCheng {
    public static void main(String[] args) {

    }
}
// 创建方法1：通过继承Thred类，实现run方法
class MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(this+"-"+i);
        }
    }
    public static void main(String[] args) {
        MyThread th1 = new MyThread();
        MyThread th2 = new MyThread();
        th1.start();
        th2.start();
        for (int i = 0; i <100; i++) {
            System.out.println(Thread.currentThread()+"-"+i);
        }
    }
}

// 创建方式2：实现Runnable 接口 ,实现run方法
// 利于扩展，可以重用
class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread()+"-"+i);
        }
    }
    public static void main(String[] args) {
        MyRunnable r = new MyRunnable();
        Thread th1 = new Thread(r);
        Thread th2 = new Thread(r);
        th1.start();
        th2.start();
    }
}
// 创建方式3：实现Callable 接口 ,实现call方法
// 一般结合线程池使用
class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            sum+=i;
        }
        return sum;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> ft = new FutureTask<>(new MyCallable() );
        Thread th = new Thread(ft);
        th.start();
        System.out.println("statrt");
        int in = ft.get();// 阻塞在这 自带jion 还可以有返回值
        System.out.println(in);
        System.out.println("end");

        ExecutorService pool = Executors.newCachedThreadPool();  // java 推荐的线程池

    }
}

/**
 * 多线程用处：   构造场景
 * 控制台倒计时10-0每秒钟会打印
 * 同时控制台还有接收控制台用户输入
 * 然后打印出来
 * (不是很好用)
 */
class TestXianCheng{
    public static void main(String[] args){

        new Thread(()->{
            for (int i = 10; i > 0; i--) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i);
            }
        }).start();

        Scanner in = new Scanner(System.in);
        System.out.println(in.next());

    }
}

class Test3{
    public static void main(String[] args) throws InterruptedException {
        Thread th1 = new Thread(()->{
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("睡醒了");
            for (int i = 0; i < 100000; i++){}
        });
        System.out.println(th1.getState());  // NEW  新建 刚刚new完
        th1.start();
        System.out.println(th1.getState());  // RUNNABLE 可运行的 有了运行的能力（线程中没有运行状态）
        // start();执行后不一定会运行函数
        Thread.sleep(2000);  //  睡眠  TIMED_WAITING  等待
        System.out.println(th1.getState());
        Thread.sleep(2101);  //
        System.out.println(th1.getState());
        Thread.sleep(1001);  // TERMINATED  停止
        System.out.println(th1.getState());
    }
}
class Test3e{
    public static void main(String[] args) {
//        Thread th = new Thread(()->{
//            while (!Thread.currentThread().isInterrupted()){
//                System.out.println("1");
//            }
//        });
//        th.interrupt(); // 中断
//        boolean in = th.isInterrupted();
//        System.out.println(in);

        Thread th = new Thread(()->{
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        th.start();
        th.interrupt(); // 视图去打断一个正在睡眠的线程，就会抛出异常
    }
}
class Testee{
    public static void main(String[] args) {
        Thread th = new Thread(()->{
        });
        th.setName("买菜线程");
        System.out.println(th.getName());
        // 设置优先级并没有决定性影响，它更多取决于操作系统的实现，没有决定性影响
        th.setPriority(Thread.MAX_PRIORITY);
        // 让步   一个先成过分占领资源，可以用此方发在某个时间点会让别人执行以下，也是没有决定性影响
        Thread.yield();
    }
}

/**
 * 守护线程
 * 用来守护其他线程，当其他线程都死了它就死了
 */
class TestDaemon{
    public static void main(String[] args) {
        // 工作线程
        Thread worker = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread()+"-"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        worker.setName("工作线程");
        // 守护线程
        Thread daemon = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread()+"-"+i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        daemon.setDaemon(true);  // 设置成守护线程
        daemon.setName("守护线程");
        worker.start();
        daemon.start();
    }
}

/**
 * 两个线程
 * 各自打印 0-999999
 * 统计一共消耗了多少毫秒
 */
class TestJoin{
    public static void main(String[] args) throws InterruptedException {
        long startTime=System.currentTimeMillis();
        Thread th1 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                System.out.println(i);
            }
        });
        Thread th2 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                System.out.println(i);
            }
        });
        th1.start();
        th2.start();
        th1.join();  // 等待子线程执行网
        th2.join();
        long endTime=System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }
}

